<?php
    //starts the session
	session_start();

	//includes files
	require_once("includes.php");	
	
	//HTTP context initialization
	HTTPContext::Initialize();
	
	//Object and configuration
	$object = new Object();
	$object->config = new Config();
	Settings::setApplicationConfiguration($object->config);
	Logger::Debug("Object created");
	
	//URL parsing
	$urlObject = new URL();
	$object->parsed_url = $object->ParseURL($urlObject->GetURL(), Settings::Get('modules'));
	HTTPContext::$parsed_url = $object->parsed_url;
	unset($urlObject);
	
	//common controller load
	if (file_exists(APP.SEPARATOR.'controller.php'))
	{
		require_once(APP.SEPARATOR.'controller.php');
	}
	else
	{
		require_once(CORE.SEPARATOR.'controller.php');
	}
	Logger::Debug("Controller included");
	
	//common model load
	if (file_exists(APP.SEPARATOR.'model.php'))
	{
		require_once(APP.SEPARATOR.'model.php');
	}
	else
	{
		require_once(CORE.SEPARATOR.SEPARATOR.'model.php');
	}
	Logger::Debug("Model included");
	
	//language selection
	if (isset($_GET['lang'])) 
	{
		$_SESSION['lang'] = $_GET['lang'];
	}
	else if (!isset($_SESSION['lang'])) 
	{
		$_SESSION['lang'] = Settings::Get('default_language');
		$_SESSION['lang_is_default'] = true;
	}
	$_SESSION['lang_short'] = substr($_SESSION['lang'], 0, 2);
	Language::setName($_SESSION['lang']);
	Language::setShortName($_SESSION['lang_short']);
	
	//language translations
	require_once(LOCALIZATION.SEPARATOR.$_SESSION[lang].SEPARATOR.'text.php');
	require_once(LANGUAGES.SEPARATOR.$_SESSION[lang].SEPARATOR.'text.php');
	
	//meta tags
	if (is_file(LANGUAGES.SEPARATOR.$_SESSION['lang'].SEPARATOR.'meta.php'))
	{
		require_once(LANGUAGES.SEPARATOR.$_SESSION['lang'].SEPARATOR.'meta.php');
	}
	else if (is_file(LANGUAGES.SEPARATOR.Settings::Get('default_language').SEPARATOR.'meta.php'))
	{
		require_once(LANGUAGES.SEPARATOR.Settings::Get('default_language').SEPARATOR.'meta.php');
	}
    Logger::Debug("Text files included");
	
	//locale
	setlocale(LC_TIME, Settings::Get('locale'));
	
	//Set custom error handler
	set_error_handler('errorMessageHandler');

	//gets the default controller and action names if needed
	$object->parsed_url['controller'] = HTTPContext::$parsed_url['controller'] = (empty(HTTPContext::$parsed_url['controller'])) ? Settings::Get('defaultController') : HTTPContext::$parsed_url['controller'];
	$object->parsed_url['action'] = HTTPContext::$parsed_url['action'] = (empty(HTTPContext::$parsed_url['action'])) ? Settings::Get('defaultAction') : HTTPContext::$parsed_url['action'];
	
	//loads Controller, Model and View
	if (!(empty(HTTPContext::$parsed_url)))
	{
		Logger::Debug("Controller load started");
		
		//controller and model load
		$current_controller = $object->LoadController($object->parsed_url['controller']);
		Logger::Debug("Controller loaded");
		
		//config into model injection
		$current_controller->controller_model->config = Settings::getApplicationConfiguration();
		
		//define IMAGES path constant according to theme used
		define('IMAGES','/./'.THEMES.SEPARATOR.$current_controller->theme.SEPARATOR.'images'.SEPARATOR);
		define('SITE_IMAGES',IMAGES.SEPARATOR.'site'.SEPARATOR);
		
		//common functionality for all pages
		$current_controller->BeforeLoad($object->parsed_url);
		
		Logger::Debug("BeforeLoad executed");
		
  		//loads helpers and component files
		if ($current_controller != null)
		{
			//Include helper files
			$current_controller->IncludeHelpersFiles();
			Logger::Debug("Helpers included");
						
			//Include components files
			$current_controller->IncludeComponentsFiles();
			Logger::Debug("Components' files included");
			
			//Unset unused variables
			unset($controller_helpers);
			unset($helper_file);
			unset($helper_name);
			unset($value);

			//initialises caching system powered by PHPSpeedy if required
			Logger::Debug('php_speedy started');
			if ((Settings::Get('use_caching'))&&($current_controller->isCached))
			{
			    require_once(CACHE.SEPARATOR.'php_speedy.php');
			}
			Logger::Debug('php_speedy ended');

			//page title
			$page_title = $current_controller->page_title;
			
			//gets page content
			Logger::Debug("View loading started");
			$page_content = $current_controller->LoadView($object->parsed_url);
			Logger::Debug("View loading ended");
			
			//Get page Layout
			include_once(THEMES.SEPARATOR.$current_controller->theme.SEPARATOR.'layouts'.SEPARATOR.$current_controller->layout.'.php');

			Logger::Debug("Layout included");
			
			unset ($current_controller->layout);
		}
	}
	else
	{
		include_once(APP.SEPARATOR.'index.php');
	}
	
	Logger::Debug("Boot ended");	
		
	//dispays logger table if it is switched on in the /app/config.php
	if (Settings::Get('use_system_logger'))
	{
		Logger::LoggerTable();
	}
	
	//finishes caching powered by PHPSpeedy if required
	Logger::Debug('compression started');
	if ((Settings::Get('use_caching')) && ($current_controller->isCached))
	{
	    $compressor->finish();
	}
	Logger::Debug('compression ended');
	
?>